package com.freeter.modules.gjs.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.freeter.modules.gjs.entity.UsersDivisions;
import com.freeter.modules.gjs.mapper.UsersDivisionsMapper;
import com.freeter.modules.gjs.service.UsersDivisionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UsersDivisionsServiceImpl implements UsersDivisionsService {
    @Autowired
    private UsersDivisionsMapper usersDivisionsMapper;

    @Override
    public List<UsersDivisions> getAllUsersDivisions() {
        Wrapper<UsersDivisions> wrapper = new EntityWrapper<>();
        wrapper.eq("is_deleted", 0);
        return usersDivisionsMapper.selectList(wrapper);
    }

    @Override
    public UsersDivisions getUsersDivisionsById(Long id) {
        return usersDivisionsMapper.selectById(id);
    }

    @Override
    public List<UsersDivisions> getUsersDivisionsByUserId(Long userId) {
        Wrapper<UsersDivisions> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", userId).eq("is_deleted", 0);
        return usersDivisionsMapper.selectList(wrapper);
    }

    @Override
    public List<UsersDivisions> getUsersDivisionsByDivisionCode(String divisionCode) {
        Wrapper<UsersDivisions> wrapper = new EntityWrapper<>();
        wrapper.eq("division_code", divisionCode).eq("is_deleted", 0);
        return usersDivisionsMapper.selectList(wrapper);
    }

    @Override
    public UsersDivisions getUsersDivisionsByUserAndDivision(Long userId, String divisionCode) {
        Wrapper<UsersDivisions> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", userId).eq("division_code", divisionCode).eq("is_deleted", 0);
        List<UsersDivisions> list = usersDivisionsMapper.selectList(wrapper);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public UsersDivisions createUsersDivisions(UsersDivisions usersDivisions) {
        usersDivisionsMapper.insert(usersDivisions);
        return usersDivisions;
    }

    @Override
    public UsersDivisions updateUsersDivisions(Long id, UsersDivisions usersDivisions) {
        usersDivisions.setId(id);
        usersDivisionsMapper.updateById(usersDivisions);
        return usersDivisions;
    }

    @Override
    public void deleteUsersDivisions(Long id) {
        usersDivisionsMapper.deleteById(id);
    }

    @Override
    public void deleteUsersDivisionsByUserAndDivision(Long userId, String divisionCode) {
        Wrapper<UsersDivisions> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", userId).eq("division_code", divisionCode);
        usersDivisionsMapper.delete(wrapper);
    }

    @Override
    public List<UsersDivisions> getUsersDivisionsByStatus(String status) {
        Wrapper<UsersDivisions> wrapper = new EntityWrapper<>();
        wrapper.eq("status", status).eq("is_deleted", 0);
        return usersDivisionsMapper.selectList(wrapper);
    }

    @Override
    public List<UsersDivisions> getUsersDivisionsByCondition(String name, Integer state) {
        Wrapper<UsersDivisions> wrapper = new EntityWrapper<>();
        if (name != null) wrapper.eq("name", name);
        if (state != null) wrapper.eq("state", state);
        wrapper.eq("is_deleted", 0);
        return usersDivisionsMapper.selectList(wrapper);
    }
} 